package com.nuc.zyt.day04.io;

import com.nuc.zyt.day03.Lambda.pojo.Album;
import com.nuc.zyt.day03.Lambda.pojo.Artist;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Io
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/5/28 9:21
 * @Version 1.0
 */

public class Io {
    public static void main(String[] args) {
        // File构造函数从项目根目录开始造（找）文件
        File file = new File("Day01/resourses/pro.properties");
        File file1 = new File("Day01/resourses/Serializable.properties");
        // 这样会在项目根目录下创建文件
        //File file1 = new File("1.txt");

        try (FileInputStream fi = new FileInputStream(file);

             FileOutputStream fo = new FileOutputStream("Day01/resourses/1.properties"); //
             FileInputStream fio = new FileInputStream("Day01/resourses/1.properties");

             ObjectOutputStream op = new ObjectOutputStream(new FileOutputStream(file1));
             ObjectInputStream io = new ObjectInputStream(new FileInputStream(file1));

             BufferedOutputStream bo = new BufferedOutputStream(fo);
             BufferedInputStream bi = new BufferedInputStream(fi);

             FilterOutputStream fot = new DataOutputStream(fo);
             //FilterInputStream fit = new DataInputStream(fo);



        ){
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
            System.out.println("文件绝对路径:"+file.getAbsolutePath());
            System.out.println("文件长度"+file.length());
            System.out.println("文件path:"+file.getPath());
            System.out.println("文件名字"+file.getName());
            System.out.println("直接读取文件内容");
//            byte[] arr = new byte[150];
//            int count = 0;
//            while ((count = fi.read(arr))!= -1){
//                System.out.println("输出内容:");
//                System.out.println(new String(arr,0,count));
//               // System.out.write(arr,0,count);
//                System.out.println("保存到文件");
//                fo.write(arr,0,10); // 会覆盖原本内容   追加内容的话需要在FileOutputStream构造函数添加 true
//            }
            // 读取文件内容
            inputFile(fi);

            System.out.println("利用缓冲区读取");





            System.out.println("序列化");

            Artist artist = new Artist();
            artist.setName("赵元通");
            artist.setMembers("185");

            List<String> collect = Stream.of("zyt", "jdisdo", "第四代", "562").collect(Collectors.toList());

            // 直接将对象序列化至文件
            op.writeObject(artist);
            // 直接从文件里反序列化
            Artist o = (Artist)io.readObject();
            System.out.println(o.getMembers()+"&&"+o.getName());

           op.writeObject(collect);

           List list = (List) io.readObject();

           list.stream().forEach(a -> System.out.println(a));



           fot.write("zyt185666".getBytes());
           inputFile(fio);










        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void inputFile(InputStream fi) throws IOException {
        byte[] arr = new byte[150];
        int count = 0;
        while ((count = fi.read(arr))!= -1){
            System.out.println("输出内容:");
            System.out.println(new String(arr,0,count));
            // System.out.write(arr,0,count);
            //System.out.println("保存到文件");
        }
    }




}
